jsx3.Class.defineClass(
        "tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance", //the full name of the class to create
        jsx3.gui.Block, //  extends
        null, // implements
        function (plugin) {
        	plugin.wsEndpointURL = tibco.admin.monitormanage.framework.mainPage.MainPage.wsEndpointURL;
        	
        	plugin.serviceSinceStartedCacheSubject = "com.tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.serviceSinceStartedCache";
        	plugin.serviceInstanceSinceStartedCacheSubject = "com.tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.serviceInstanceSinceStartedCache";
        	
        	plugin.serviceSinceStartedCacheId = "SPM_SERVICE_SINCE_STARTED_CACHE";
        	plugin.serviceInstanceSinceStartedCacheId = "SPM_SERVICE_INSTANCE_SINCE_STARTED_CACHE";
        	
        	plugin.prototype.isServiceInstanceMode;
        	plugin.prototype.screenId;
        	plugin.prototype.serviceId;
        	plugin.prototype.serviceInstanceId;
        
            plugin.prototype.init = function() {
                this.jsxsuper();
            }
            
            plugin.prototype.getNamespace = function() {
               return "tibco.admin";
            }
            
            plugin.prototype.loadComponent = function(isServiceInstanceMode) {
            	this.isServiceInstanceMode = isServiceInstanceMode;
            	
            	var screenId;
            	if(isServiceInstanceMode)
            	{ screenId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceScreenId;
                }
            	else
            	{ screenId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceScreenId; }
            	tibco.admin.monitormanage.general.PollingHandler.register(tibco.admin.monitormanage.general.PollingHandler.GENERAL_POLLING_HANDLER_ID, this, this.updateComponent, screenId);
            	tibco.admin.monitormanage.general.RefreshHandler.register(this, this.updateComponent, screenId);
            	this.screenId = screenId;
            	
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.general.NavigationHandler.mainPageDisplayTopic, this.handleDisplayScreen, this);
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(tibco.admin.monitormanage.uicomponents.dropdown.timeframe.Timeframe.selectedTimeframeSubject, this.timeframeSelected, this);
            	
            	var subject;
            	if(isServiceInstanceMode)
            	{ subject = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceInfoCacheSubject; }
            	else
            	{ subject = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInfoCacheSubject; }
            	tibco.admin.monitormanage.general.PageBusUtils.subscribe(subject, this.updatePerformanceStats, this);
            
            	var timeComparisonPane = this.getDescendantOfName("timeComparisonPane");
        		if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.table || !tibco.admin.monitormanage.uicomponents.table.timeComparison || !tibco.admin.monitormanage.uicomponents.table.timeComparison.serviceStats || !tibco.admin.monitormanage.uicomponents.table.timeComparison.serviceStats.ServiceStats)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/table/timeComparison/serviceStats/ServiceStats.js", this.getNamespace()); }
                var timeCompObj = tibco.admin.Main.loadUIResource(timeComparisonPane, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/table/timeComparison/serviceStats/components/serviceStats.xml", true);
                timeCompObj.setUpComponent(isServiceInstanceMode);
            
                /*var requestsPiePane1 = this.getDescendantOfName("bottomLeftPane");
        		if(!tibco.admin.monitormanage || !tibco.admin.monitormanage.uicomponents || !tibco.admin.monitormanage.uicomponents.chart || !tibco.admin.monitormanage.uicomponents.chart.serviceRequestsPie || !tibco.admin.monitormanage.uicomponents.chart.serviceRequestsPie.ServiceRequestsPie)
                { tibco.admin.Main.loadClass("tibco/admin/monitormanage/uicomponents/chart/serviceRequestsPie/ServiceRequestsPie.js", this.getNamespace()); }
                var requestsPieModel1 = tibco.admin.Main.loadUIResource(requestsPiePane1, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/chart/serviceRequestsPie/components/serviceRequestsPie.xml", true);   
                var chartId = "serviceRequestsPieChartId1";
                if(isServiceInstanceMode)
                { chartId = "serviceInstanceRequestsPieChartId1"; }
                requestsPieModel1.setUpComponent(chartId, true, isServiceInstanceMode);
                
                var requestsPiePane2 = this.getDescendantOfName("bottomRightPane");
                var requestsPieModel2 = tibco.admin.Main.loadUIResource(requestsPiePane2, this.getNamespace(), "tibco/admin/monitormanage/uicomponents/chart/serviceRequestsPie/components/serviceRequestsPie.xml", true);   
                chartId = "serviceRequestsPieChartId2";
                if(isServiceInstanceMode)
                { chartId = "serviceInstanceRequestsPieChartId2"; }
                requestsPieModel2.setUpComponent(chartId, false, isServiceInstanceMode);
               this.getDescendantOfName("performanceHelpImg").setEvent("this.getAncestorOfName('performanceBlk').performanceHelpImgClicked(objEVENT)", "jsxexecute");
               this.getDescendantOfName("faultsuccessHelpImg").setEvent("this.getAncestorOfName('performanceBlk').faultsuccessHelpImgClicked(objEVENT)", "jsxexecute");*/

            }

             plugin.prototype.performanceHelpImgClicked =function (objEVENT){
               if(this.isServiceInstanceMode)
             this.getDescendantOfName("performanceHelpImg").openHelp("MonitorManage.Service.Instance.Performance  ","tibco.admin",objEVENT);
               else
             this.getDescendantOfName("performanceHelpImg").openHelp("MonitorManage.Service.Integrated.Performance ","tibco.admin",objEVENT);
           }

              plugin.prototype.faultsuccessHelpImgClicked =function (objEVENT){
              if(this.isServiceInstanceMode)
             this.getDescendantOfName("faultsuccessHelpImg").openHelp("MonitorManage.Service.Instance.FaultsSuccesses  ","tibco.admin",objEVENT);
              else
             this.getDescendantOfName("faultsuccessHelpImg").openHelp("MonitorManage.Service.Integrated.FaultSuccesses  ","tibco.admin",objEVENT);
           }
            
            plugin.prototype.updateComponent = function() {
        		if(this.isServiceInstanceMode)
            	{ this.callGetServiceInstances(); }
            	else
            	{
                    this.callGetServices(); }
			}
			
			plugin.prototype.timeframeSelected = function(subject, message, servicePerfObj) {
				servicePerfObj.updateComponent();
			}
			
			plugin.prototype.handleDisplayScreen = function(subject, message, servicePerfObj) {

                if(message.screenId == servicePerfObj.screenId)
            	{
                   
                    if(servicePerfObj.isServiceInstanceMode)
            		{ servicePerfObj.serviceInstanceId = message.serviceInstanceId; }
            		else
            		{ servicePerfObj.serviceId = message.serviceId; }
                    
                    servicePerfObj.updateComponent();
            	}

            }
            
            //call this method to begin the service call (eg.service.callgetServices();)
    		plugin.prototype.callGetServices = function() {
                  if(tibco.admin.Main.getServer().getEnv("MODE"))
    			{
					// mode is live
					var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/serviceView/rules/serviceTimeframeRules.xml", "getServices");
					objService.setEndpointURL(plugin.wsEndpointURL);
					// this setInboundURL should not be required...only here because of a current AMX framework bug
					objService.setInboundURL("tibco/admin/monitormanage/uicomponents/page/serviceView/data/serviceScreenTimeframeData.xml");
				}
				else 
				{
					var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/serviceView/rules/serviceTimeframeRules.xml"), "getServices", null, null);
					// mode is static
					objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/serviceView/data/serviceScreenTimeframeData.xml"));
				}
				
				objService.servicePerfObj = this;
				objService.serviceId = this.serviceId;

				// initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("startDate");
				metricsList.add("availability");
				metricsList.add("uptime");
				metricsList.add("throughput");
				metricsList.add("avgResponseTime");
				metricsList.add("hitCount");
				metricsList.add("faults");
				metricsList.add("successes");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetServicesSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetServicesError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetServicesInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetServicesSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
     			//jsx3.log("Success","The service call was successful.");
     			var message = { initialized:true };
        		tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.serviceSinceStartedCacheSubject, message);
    		};

    		plugin.prototype.ongetServicesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetServicesInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};
        	
        	//call this method to begin the service call (eg.service.callgetServiceInstances();)
    		plugin.prototype.callGetServiceInstances = function() {
      			if(tibco.admin.Main.getServer().getEnv("MODE")) 
    			{
					// mode is live
					var objService = tibco.admin.Main.loadService(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/serviceView/rules/serviceInstanceTimeframeRules.xml", "getServiceInstances");
					objService.setEndpointURL(plugin.wsEndpointURL);
					// this setInboundURL should not be required...only here because of a current AMX framework bug
					objService.setInboundURL("tibco/admin/monitormanage/uicomponents/page/serviceView/data/serviceInstanceScreenTimeframeData.xml");
				}
				else 
				{
					var objService = new jsx3.net.Service(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/serviceView/rules/serviceInstanceTimeframeRules.xml"), "getServiceInstances", null, null);
					// mode is static
					objService.setInboundURL(tibco.admin.Main.resolveURI(this.getNamespace(), "tibco/admin/monitormanage/uicomponents/page/serviceView/data/serviceInstanceScreenTimeframeData.xml"));
				}
				
				objService.servicePerfObj = this;
				objService.serviceInstanceId = this.serviceInstanceId;
				
				// initialize the list of metrics we wish to retrieve data for
				var metricsList = new jsx3.util.List();
				metricsList.add("startDate");
				metricsList.add("availability");
				metricsList.add("uptime");
				metricsList.add("throughput");
				metricsList.add("avgResponseTime");
				metricsList.add("hitCount");
				metricsList.add("faults");
				metricsList.add("successes");
				objService.metricsList = metricsList;

      			//subscribe and call
      			objService.subscribe(jsx3.net.Service.ON_SUCCESS, this.ongetServiceInstancesSuccess);
      			objService.subscribe(jsx3.net.Service.ON_ERROR, this.ongetServiceInstancesError);
      			objService.subscribe(jsx3.net.Service.ON_INVALID, this.ongetServiceInstancesInvalid);
      			objService.doCall();
    		};

    		plugin.prototype.ongetServiceInstancesSuccess = function(objEvent) {
      			//var responseXML = objEvent.target.getInboundDocument();
      			//jsx3.log("Success","The service call was successful.");
      			//alert(tibco.admin.Main.getServer().Cache.getDocument(plugin.serviceInstanceSinceStartedCacheId));
      			var message = { initialized:true };
        		tibco.admin.monitormanage.general.PageBusUtils.publish(plugin.serviceInstanceSinceStartedCacheSubject, message);
    		};

    		plugin.prototype.ongetServiceInstancesError = function(objEvent) {
      			var myStatus = objEvent.target.getRequest().getStatus();
      			jsx3.log("Error","The service call failed. The HTTP Status code is: " + myStatus);
    		};

    		plugin.prototype.ongetServiceInstancesInvalid = function(objEvent) {
      			jsx3.log("Invalid","The following message node just failed validation:\n\n" + objEvent.message);
    		};
    		
    		plugin.prototype.updatePerformanceStats = function(subject, message, servicePerfObj) {
    			var cachedDocId;
    			if(servicePerfObj.isServiceInstanceMode)
    			{ cachedDocId = plugin.getCacheInfoId(false,true); }
    			else
    			{ cachedDocId = plugin.getCacheInfoId(false,false); }
    			var cachedDoc = tibco.admin.Main.getServer().Cache.getDocument(cachedDocId);
    			var record = cachedDoc.getFirstChild();
    			
    			var startDateField = servicePerfObj.getDescendantOfName("startDateValueLabel");
	            var startDateLong = record.getAttribute("startDate");
			var startDate;
			if(startDateLong == 0)
			{ startDate = tibco.admin.Main.getServer().getDynamicProperty("@MM@general@notApplicable"); }
			else
			{ startDate = tibco.admin.monitormanage.general.MonitorManageUtils.formatStartDateISO(startDateLong); }
    			startDateField.setText(startDate, true);

				var availabilityField = servicePerfObj.getDescendantOfName("availabilityValueLabel");
                var availability =record.getAttribute("availability");
            availabilityField.setText(availability, true);
            var availabilityFieldVal = parseInt(availability);
                if (availabilityFieldVal <= 33)
                {
                    availabilityField.setBackgroundColor(tibco.admin.Main.getServer().getDynamicProperty("@MM@general@lblServiceAvailabilityBg1"));
                }
                else if (availabilityFieldVal > 33 && availabilityFieldVal <= 66)
                {
                    availabilityField.setBackgroundColor(tibco.admin.Main.getServer().getDynamicProperty("@MM@general@lblServiceAvailabilityBg2"));
                } else if (availabilityFieldVal > 66)
                {
                    availabilityField.setBackgroundColor(tibco.admin.Main.getServer().getDynamicProperty("@MM@general@lblServiceAvailabilityBg3"));
                }
                availabilityField.repaint();
    			var uptimeField = servicePerfObj.getDescendantOfName("uptimeValueLabel");
    			var uptime = record.getAttribute("uptime");
    			uptime = tibco.admin.monitormanage.general.MonitorManageUtils.formatElapsedTime(uptime);
    			uptimeField.setText(uptime, true);
    			uptimeField.repaint();
    		}
    		
    		plugin.getCacheInfoSubject = function(isSinceStarted, isServiceInstance) {
    			var subject;
            	if(isSinceStarted)
            	{
            		if(isServiceInstance)
            		{ subject = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.serviceInstanceSinceStartedCacheSubject; }
            		else
            		{ subject = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.serviceSinceStartedCacheSubject; }            		
            	}
            	else
            	{
            		if(isServiceInstance)
            		{ subject = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceInfoCacheSubject; }
            		else
            		{ subject = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInfoCacheSubject; }
            	}
            	return subject;
            }
            
            plugin.getCacheInfoId = function(isSinceStarted, isServiceInstance) {
            	var cacheId;
            	if(isSinceStarted)
            	{
            		if(isServiceInstance)
            		{ cacheId = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.serviceInstanceSinceStartedCacheId; }
            		else
            		{ cacheId = tibco.admin.monitormanage.uicomponents.misc.servicePerformance.ServicePerformance.serviceSinceStartedCacheId; }
            	}
            	else
            	{
            		if(isServiceInstance)
            		{ cacheId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInstanceInfoCacheId; }
            		else
            		{ cacheId = tibco.admin.monitormanage.uicomponents.page.serviceView.ServiceView.serviceInfoCacheId; }            		
            	}
            	return cacheId;
            }

        });
